﻿using System;
using System.Collections.Generic;
using System.Text;
using Estimate.Common.Entity;
using Estimate.Common.Estimate;
using Estimate.Common.Repository;
using Estimate.Common.DBFieldTypes;
using System.Text.RegularExpressions;

namespace Estimate.Reports
{
    public class BaseReport
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string connectionString;

        /// <summary>
        /// 实况数据数据库访问对象
        /// </summary>
        protected RealDataLogRepository realDataRepository;

        public BaseReport(string connString)
        {
            connectionString = connString;
            realDataRepository = new RealDataLogRepository(connString);
        }

        /// <summary>
        /// 查看数据库的RealDataLog表，查看指定的时间段数据是否存在
        /// 如果存在，即表示这个查询数据是合理的，予以缓存入表
        /// </summary>
        /// <param name="beginDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>是否合适</returns>
        public bool IsValideDateSpan(ForeSemiType realtype, DateTime beginDate, DateTime endDate)
        {
            return
            realDataRepository.IsRealDataExist(realtype, beginDate) && realDataRepository.IsRealDataExist(realtype, endDate);
        }

        /// <summary>
        /// 计算综合分
        /// </summary>
        /// <param name="pc_24">24小时晴雨(pc)</param>
        /// <param name="normal_24">24小时一般性降水</param>
        /// <param name="pc_48">48小时晴雨(pc)</param>
        /// <param name="normal_48">48小时一般性降水</param>
        /// <param name="maxt2_24">24小时最高温度2度误差</param>
        /// <param name="mint2_24">24小时最低温度2度误差</param>
        /// <param name="maxt2_48">48小时最高温度2度误差</param>
        /// <param name="mint2_48">24小时最低温度2度误差</param>
        /// <param name="count_24">24小时预报样本数</param>
        /// <param name="count_48">48小时预报样本数</param>
        /// <returns>计算出的综合分</returns>
        public float CalculateComposition(float pc_24, float normal_24, float pc_48, float normal_48,
            float maxt2_24, float mint2_24, float maxt2_48, float mint2_48, int count_24, int count_48)
        {
            if (count_24 == 0 && count_48 == 0)
                return 0f;

            pc_24 = pc_24 == 101f ? 0f : pc_24;

            normal_24 = normal_24 == 101f ? 0f : normal_24;

            pc_48 = pc_48 == 101f ? 0f : pc_48;

            normal_48 = normal_48 == 101f ? 0f : normal_48;
            
            //float a = count_24 * 1f / (count_24 + count_48);

            //float b = count_48 * 1f / (count_24 + count_48);

            //float score = 0.15f * pc_24 * a + 0.25f * normal_24 * a + 0.1f * pc_48 * b + 0.2f * normal_48 * b +
            //    0.1f * maxt2_24 * a + 0.1f * mint2_24 * a + 0.1f * maxt2_48 * a + 0.1f * mint2_48 * a;

            //0-24小时晴雨*0.15+0-24小时降水TS*0.25+24-48小时晴雨*0.1+24-48小时降水TS*0.2+0-24小时最高温度2度误差准确率*0.1+0-24小时最低温度2度误差准确率*0.1+24-48小时最高温度2度误差准确率*0.05+24-48小时最低温度2度误差准确率*0.05
            float score = 0.15f * pc_24  + 0.25f * normal_24  + 0.1f * pc_48  + 0.2f * normal_48  +
                0.1f * maxt2_24 + 0.1f * mint2_24 + 0.05f * maxt2_48 + 0.05f * mint2_48;

            return score;
        }

    }
}
